'ui';
importClass(java.util.Calendar)
importClass(java.text.SimpleDateFormat)


ui.statusBarColor('#40a5f3')
ui.layout(
	<vertical>
		<appbar bg="#40a5f3">
			<toolbar id="title" title="融易注册机" />
		</appbar>
		<vertical w="*" h="auto" marginTop="40" gravity="center">
            <radiogroup gravity="center" orientation="horizontal">
                <radio id="zk" checked="true" text="周卡" />
                <radio id="yk" text="月卡"/>
            </radiogroup>
            <input id="imeiKey" hint="设备标识码" gravity="center" textSize="12sp" margin="20 10" />
            <frame h="30" w="auto" layout_gravity="center" marginTop="2">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					radius="6dp"
				/>
				<text
					id="start"
					h="30"
					text="生成激活码"
                    textSize="12sp"
                    textColor="#000000"
                    gravity="center"
                    margin="60 0" 
				/>
			</frame>
		</vertical>
	</vertical>
)

function createKeyCode(imeiKey, deadline) {
	let message = parseInt(deadline) * 3 - 8 + imeiKey
	let aesKey = new $crypto.Key('u==抠你🐔娃')
	let aes = $crypto.encrypt(message, aesKey, 'AES/ECB/PKCS5padding')
	let keyCode = $crypto.digest($base64.encode(aes) + 'rong', 'MD5').toString()
	return keyCode
}


ui.start.on('click', function () { 
    let zk = ui.zk.checked
    let yk = ui.yk.checked
    let imeiKey = ui.imeiKey.text().toString()
    let calendar = Calendar.getInstance();
    let sdf = new SimpleDateFormat('yyMMdd');
    if (zk) { 
        calendar.add(Calendar.DATE, 7);
        let mDate = sdf.format(calendar.getTime())
        let keyCode = createKeyCode(imeiKey,mDate)
        setClip(keyCode)
        toastLog('周卡：激活码已复制到剪切板')
        return
    }
    if (yk) { 
        calendar.add(Calendar.DATE, 30);
        let mDate = sdf.format(calendar.getTime())
        let keyCode = createKeyCode(imeiKey, mDate)
        setClip(keyCode)
        toastLog('月卡：激活码已复制到剪切板')
        return
    }
})
// let keyCode = createKeyCode(imeiKey,200830)
